<?php
namespace Home\Controller;
use Think\Controller;
class CityController extends Controller{
	//首页
	public function index(){
// 		$city_num = M("Place")->where("is_show")->count();
// 		$this->assign("city_num",$city_num);
		$this->display();
	}
	public function getShowCity(){
		$is_show = isset($_REQUEST["is_show"]) ? $_REQUEST["is_show"] : 1;
		$city = M("Place")->where("is_show = $is_show")->select();
		for($i=0;$i<count($city);$i++){
			$id = $city[$i]["id"];
			$city[$i]["caozuo"]="<a href='/Home/City/edit?id=$id'>修改排序</a>|";
			$city[$i]["caozuo"].="<a href='/Home/City/cancelCity?id=$id' onclick='return confirm(\"确认取消？？\")'>取消开通</a>";
		}
		echo json_encode($city);
	}
	//修改排序
	public function edit(){
		$id = $_REQUEST['id'];
		$city =  M("Place")->where("id = $id")->find();
		$this->assign("city",$city);
		$this->display();
		
	}
	
	public function update(){
		
		$id = $_REQUEST["id"];
		$sort = $_REQUEST['sort'];
		if($sort == "" || $sort < 0){
			$this->error("排序不能为空或要为正整数！","/Home/City/edit?id=$id");
		}
		$place = M("Place");
		$place->sort = $sort;
		if($place->where("id = $id")->save()){
			$this->success("修改成功!",index);
		}else{
			$this->error("修改失败!","/Home/City/edit?id=$id");
		}
	}
	
	//取消
	public function	cancelCity(){
		$id = $_REQUEST["id"];
		$place = M("Place");
		$place->is_show = 0;
		if($place->where("id = $id")->save()){
			$this->success("取消成功",index);
		}else{
			$this->error("修改失败",index);
		}
	}

	//添加
	public function addCity(){
		$list=array();
		$num = 0;
		$city_1 = M("Place")->where("level =1")->select();
		$city_2 = M("Place")->where("level =2")->select();
		for($i=0;$i<count($city_1);$i++){
			$list[$city_1[$i]['code']][0] = $city_1[$i];
		}
		for($i=0;$i<count($city_2);$i++){
			$list[$city_2[$i]['parentid']][] = $city_2[$i];
		}
//		$list[11][]=$city_1[0];
//		$list[12][]=$city_1[1];
// 		dump($city_1);
// 		dump($list);
// 		exit;
		for($i=0;$i<count($city_1);$i++){
			$str = "";
// 			echo $city_1[$i]['code']."/".$i."<br/>";
			for($k=0;$k<count($list[$city_1[$i]['code']]);$k++){
					
				if($list[$city_1[$i]['code']][$k]['is_show']){
					$str .=	'<li><a class="" href="/Home/City/cancelCity?id='.$list[$city_1[$i]['code']][$k]['id'].'">
								<span class="glyphicon glyphicon-share-alt"></span> '.$list[$city_1[$i]['code']][$k]['name'].'
							</a>-----------
							<a href="/Home/City/cancelCity?id='.$list[$city_1[$i]['code']][$k]['id'].'">取消</a></li>';
				}else{
					$str .=	'<li><a class="" href="/Home/City/insert?id='.$list[$city_1[$i]['code']][$k]['id'].'">
								<span class="glyphicon glyphicon-share-alt"></span> '.$list[$city_1[$i]['code']][$k]['name'].'
							</a>-----------
							<a href="/Home/City/insert?id='.$list[$city_1[$i]['code']][$k]['id'].'">添加</a></li>';
				}
				
			}
			$city_1[$i]['list'] = $str;
		}
// 		dump($city_1);exit;
		$this->assign("city",$list);
		$this->assign("pro",$city_1);
		$this->display();
		
	}
	
	public function insert(){
		$id = $_REQUEST["id"];
		$place = M("Place");
		$place->is_show = 1;
		if($place->where("id = $id")->save()){
			$this->success("添加成功！",addCity);
		}else{
			$this->error("修改失败！",addCity);
		}
	}
	
}